package com.gateway.enter.controller;

import com.gateway.enter.entity.Student;
import com.gateway.enter.service.StudentService;
import com.gateway.enter.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author KLC0422
 * @create 2021-12-26- 9:46
 */
@Controller
public class LoginController {

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @GetMapping("/login")
    public String getLogin(){
        return "site/login";
    }

    @Autowired
    private StudentService studentService;


    @PostMapping("/login")
    public String login(Student student1, Map<String,Object> map, HttpServletRequest request, HttpServletResponse response){
        String emailAddress = student1.getEmailAddress();
        String password = student1.getPassword();
        Student s1 =new Student();
        s1.setEmailAddress(emailAddress);
        Student student = studentService.getStudentByEmailAddress(s1);
        if(student == null){
            map.put("msg","没有该学生");
            return "site/login";
        }
        String password1 = student.getPassword();
        String salt = student.getSalt();
        String password2 = password+salt;
        String s = PasswordUtil.password2Md5(password2);
        if(!s.equals(password1)){
            map.put("msg","用户名或密码不正确");
            return "site/login";
        }
        if(student.getActive() == 0){
            map.put("msg","您还未激活，请联系管理员");
            return "site/login";
        }
        String address = student.getAddress();
        HttpSession session = request.getSession();
        session.setAttribute("student",student);
        map.put("student",student);
        return "site/home";
    }

}
